﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics.Contracts;
using System.Windows;
using System.ComponentModel;

namespace MoneyManager
{
    public enum TransactionType { Expense, Income }

    public class Transaction : ICloneable, INotifyPropertyChanged
    {
        public int Id { get; set; }
        public Category Category { get; set; }
        public String Description { get; set; }
        public Double? Amount { get; set; }
        public Double? AmountAbs
        { 
            get
            {
                return (Double?)Math.Abs((Decimal)Amount);
            }
            set
            {
                Amount = (Type == TransactionType.Expense ? -1 : 1) * value;
            } 
        }

        public DateTime Date { get; set; }

        public String Notes { get; set; }
        public TransactionType Type { get; set; }
        public String Currency { get; set; } 

        public Transaction() 
        {
            Id = ConstantsAndDefaultValuesClass.ObjectIsNotInDb;
            Type = TransactionType.Expense;
            Date = DateTime.Now;
        }

        public bool Validate()
        {
            try
            {
                if (Category == null) throw new Exception("Category name should be set");
                if (Amount == null) throw new Exception("Amount should be set");
                if (Date == null) throw new Exception("Date should be set");
                //if (Currency == null) throw new Exception("Currency shoul be set");
                if (Description == null) throw new Exception("Description should be set");
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "Money Manager");
                return false;
            }
            return true;
        }

        public void CopyFields(Transaction transaction)
        {
            this.Category = transaction.Category;
            this.Description = transaction.Description;
            this.Amount = transaction.Amount;
            this.Date = transaction.Date;
            this.Currency = transaction.Currency;
            this.Notes = transaction.Notes;
            this.Type = transaction.Type;
            RefreshAllProperties();
        }

        private static String[] PropertiesForRefreshing = { "Category", "Description", "Amount", "Date", "Currency", "Notes", "Type", "AmountWithCurrencySymbol", "AbsAmount" };
        private void RefreshAllProperties()
        {
            foreach (String s in PropertiesForRefreshing)
                NotifyPropertyChanged(s);
        }

        #region ICloneable Members

        public object Clone()
        {
            return this.MemberwiseClone();
        }

        #endregion

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion

        public override bool Equals(object obj)
        {
            Transaction trans = obj as Transaction;
            if (trans == null)
                return false;
            return Id == trans.Id && Category == trans.Category && Description == trans.Description &&
                Amount == trans.Amount && Notes == trans.Notes && Currency == trans.Currency;
        }
    }
}
